package com.ubs.training.server.dao.impl.jdbc;

import com.ubs.training.server.dao.ContactDao;
import com.ubs.training.server.dao.model.Client;
import com.ubs.training.server.dao.model.Contact;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import javax.sql.DataSource;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xubuntu
 * Date: 8/21/12
 * Time: 4:11 AM
 * To change this template use File | Settings | File Templates.
 */
public class NamedParamContactDaoImpl implements ContactDao {

    public NamedParameterJdbcOperations getOps() {
        return ops;
    }

    public void setOps(NamedParameterJdbcOperations ops) {
        this.ops = ops;
    }

    private DataSource ds;

    private NamedParameterJdbcOperations ops;


    public DataSource getDs() {
        return ds;
    }

    public void setDs(DataSource ds) {
        this.ds = ds;
        ops = new NamedParameterJdbcTemplate(ds);
    }

    @Override
    public List<Contact> getAll() {
        return ops.query("select * from contact", Collections.EMPTY_MAP, new BeanPropertyRowMapper(Contact.class));
    }

    @Override
    public List<Contact> getAllContactsOfClient(int id) {
        return ops.query("select * from contact where clientID=:id", Collections.singletonMap("id", id), new BeanPropertyRowMapper(Contact.class));
    }

    @Override
    public List<Contact> getAllContactsOfClient(Client cl) {
        return getAllContactsOfClient(cl.getId());
    }

    @Override
    public Contact getContactByID(int id) {
        return ops.queryForObject("select * from contact where contactID=:contactID", Collections.singletonMap("contactID", id),
                new BeanPropertyRowMapper<Contact>(Contact.class));
    }

    @Override
    public void add(Contact contact) {

        int id = ops.queryForInt("select next value for cmstr_seq from dual", Collections.EMPTY_MAP);
        contact.setContactID(id);
        ops.update("insert into contact values (:contactID, :clientID, :countryID, :city, :district, :street, :apartments, :activeStatus)", new BeanPropertySqlParameterSource(contact));

    }

    @Override
    public void update(Contact contact) {
        ops.update("update contact set clientID=:clientID," +
                " countryID=:countryID," +
                " city=:city," +
                " district=:district," +
                " street=:street," +
                " apartments=:apartments," +
                " activeStatus=:activeStatus" +
                " where contactID=:contactID ", new BeanPropertySqlParameterSource(contact));
    }

    @Override
    public void removeContact(Contact cont) {
        //removeContact(cont.getContactID());
        ops.update("delete from contact where contactID=:contactID", new BeanPropertySqlParameterSource(cont));
    }

    @Override
    public void removeContact(int id) {
        ops.update("delete from contact where contactID=:id", Collections.singletonMap("id", id));
    }


}
